package com.ubis.ibms.plugin.composites.pay;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.util.SqlUtil;
import com.ubis.ibms.plugin.common.verify.PayBillQueryVerify;
import com.ubis.ibms.plugin.common.viewer.ComboLabelProvider;
import com.ubis.ibms.plugin.common.viewer.TableContentProvider;
import com.ubis.ibms.plugin.common.viewer.TableLabelProvider;
import com.ubis.ibms.plugin.dao.BillManager;
import com.ubis.ibms.plugin.dao.EstateManager;
import com.ubis.ibms.plugin.dialogs.CompanyChooseDialog;
import com.ubis.ibms.plugin.dialogs.PropertyAddDialog;
import com.ubis.ibms.plugin.model.BillLog;
import com.ubis.ibms.plugin.model.Building;
import com.ubis.ibms.plugin.model.Company;
import com.ubis.ibms.plugin.model.Floor;
import com.ubis.ibms.plugin.model.Room;
import com.ubis.ibms.plugin.model.SeqGenereter;

/**
 * 消费结算
 * @author diamond-wang
 *
 */
public class PayAddComposite extends Composite {
	
	/*交物业费*/
	private Button propertyButton;
	/* 公司id */
	private Text idText;
	/* 公司编号 */
	private Text nameText;
	// 选择公司
	private Button chooseCompanyButton;
	// 结算项目
	private Combo itemCombo;
	// 楼栋
	private ComboViewer buildingComboViewer;
	private Combo buildingCombo;
	// 楼层
	private ComboViewer floorComboViewer;
	private Combo floorCombo;
	// 房间
	private ComboViewer roomComboViewer;
	private Combo roomCombo;
	// 查询
	private Button queryButton;
	// 付费
	private Button payButton;

	private TableViewer tableViewer;
	private Table table;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public PayAddComposite(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		setLayout(layout);
		
		Label title = new Label(this, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("消费结算");

		Composite payComposite = new Composite(this, SWT.BORDER);
		payComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		payComposite.setLayout(new GridLayout(6, false));

		// 公司名称
		Label nameLabel = new Label(payComposite, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("公司名称：");
		nameText = new Text(payComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		nameText.setEditable(false);
		
		// 选择公司
		chooseCompanyButton = new Button(payComposite, SWT.NONE);
		chooseCompanyButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				true, false, 1, 1));
		chooseCompanyButton.setText("选择公司");
		//点击弹出选择公司窗口
		chooseCompanyButton.addMouseListener(new MouseAdapter(){
			public void mouseUp(MouseEvent e) {
				CompanyChooseDialog companyDialog = new CompanyChooseDialog(PayAddComposite.this.getShell());
				companyDialog.create();
				if (companyDialog.open() == Window.OK) {
					Company company = companyDialog.getCompany();
					if(company!=null){
						idText.setText(String.valueOf(company.getId()));
						nameText.setText(company.getName());
						
						buildingComboViewer.setInput(null);
						floorComboViewer.setInput(null);
						roomComboViewer.setInput(null);
						itemCombo.setText("");
						tableViewer.setInput(null);
					}
				}
			}
		});
		
		//交物业费
		propertyButton = new Button(payComposite, SWT.NONE);
		propertyButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				false, false, 1, 1));
		propertyButton.setText("交物业费");
		//点击弹出选择公司窗口
		propertyButton.addMouseListener(new MouseAdapter(){
			public void mouseUp(MouseEvent e) {
				IStructuredSelection selection = (IStructuredSelection) roomComboViewer
						.getSelection();
				if(selection.isEmpty()){//未选中房间
					MessageDialog.openWarning(PayAddComposite.this.getShell(), "警告",
							"请先选择房间！");
				}
				else{
					Room room = (Room)selection.getFirstElement();
					PropertyAddDialog addDialog = new PropertyAddDialog(PayAddComposite.this.getShell(), room);
					addDialog.open();
				}
				
			}
		});
		//公司moi id
		idText = new Text(payComposite, SWT.BORDER);
		idText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		idText.setEditable(false);
		idText.setVisible(false);

		// 楼栋
		Label buildingLabel = new Label(payComposite, SWT.RIGHT);
		buildingLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		buildingLabel.setText("楼栋：");
		buildingComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		buildingCombo = buildingComboViewer.getCombo();
		buildingCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		buildingComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		buildingComboViewer.setLabelProvider(new ComboLabelProvider());
		// 获取楼数据
		List<Building> buildings = EstateManager.getBuildings();
		buildingComboViewer.setInput(buildings);

		buildingComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						String buildingCode = ((Building) selection
								.getFirstElement()).getEms_id();
						// 获取楼层数据
						List<Floor> floors = EstateManager
								.getFloorsByBuilding(buildingCode);
						floorComboViewer.setInput(floors);
						roomComboViewer.setInput(null);
						itemCombo.setText("");
					}
				});

		// 楼层
		Label floorLabel = new Label(payComposite, SWT.RIGHT);
		floorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		floorLabel.setText("楼层：");
		floorComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		floorCombo = floorComboViewer.getCombo();
		floorCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		floorComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		floorComboViewer.setLabelProvider(new ComboLabelProvider());

		floorComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						if(selection.isEmpty())
							return;
						//获取楼编号
						IStructuredSelection buildingSelection = (IStructuredSelection) buildingComboViewer
								.getSelection();
						String buildingCode = ((Building) buildingSelection.getFirstElement())
								.getEms_id();
						//获取层编号
						String floorCode = ((Floor) selection.getFirstElement())
								.getEms_id();
						// 获取楼层数据
						List<Room> rooms = EstateManager.getRoomsByBuildingAndFloor(buildingCode, floorCode, true);
						roomComboViewer.setInput(rooms);
					}
				});

		// 房间号
		Label roomLabel = new Label(payComposite, SWT.RIGHT);
		roomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		roomLabel.setText("房间：");
		roomComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		roomCombo = roomComboViewer.getCombo();
		roomCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		roomComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		roomComboViewer.setLabelProvider(new ComboLabelProvider());
		roomComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
					}
				});

		// 结算项目
		Label itemLabel = new Label(payComposite, SWT.RIGHT);
		itemLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		itemLabel.setText("结算项目：");
		itemCombo = new Combo(payComposite, SWT.NONE);
		itemCombo.setItems(new String[] { "","电费","动力电费", "照明电费","水费" });
		itemCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		
		new Label(payComposite, SWT.NONE);
		// 查询按钮
		queryButton = new Button(payComposite, SWT.NONE);
		queryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		queryButton.setText("查询");
		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				IStructuredSelection selection = (IStructuredSelection) roomComboViewer
						.getSelection();
				Room room = (Room) selection.getFirstElement();
				Long room_moi_id = null;
				Long company_moi_id = null;
				if (room != null) {
					room_moi_id = room.getId();
				}
				if (idText.getText() != null && idText.getText().length() != 0) {
					company_moi_id = Long.valueOf(idText.getText());
					if (company_moi_id == 0) {
						company_moi_id = null;
					}
				}
				String pay_type = itemCombo.getText();
				int state = PayBillQueryVerify.verify(company_moi_id,room_moi_id);
				// 0:表单填写不合法。 1：填写了公司，没写房间 。2：填写房间没写公司。3：都填写了
				//查询某公司或某房间应该 结算的费用，指的是，在账单表中查询该公司或房间未付的账单
				//如果查询条件合法，则首先保存上次结账时间点到本次查询时间点之间的账单，
				//然后在给予查询显示出所有未付的账单，这样可用于手动结算，结算到查询点的账单。
				SeqGenereter.clearseq();
				switch (state) {
				case 0:
					MessageDialog.openWarning(PayAddComposite.this.getShell(), "警告",
							"您输入的查询条件不合法");
					break;
				case 1:
					BillManager.saveBillLogsbyCompany_moi_id(company_moi_id);
					ArrayList<BillLog> BLogs = BillManager.getFalseBillLogsbyCompany_moi_id(company_moi_id,pay_type);
					if (BLogs == null || BLogs.size() == 0) {
						tableViewer.setInput(BLogs);
						MessageDialog.openInformation(PayAddComposite.this.getShell(), "通知",
								"没有找到未付的账单！");
					} else {
						tableViewer.setInput(BLogs);
					}
					break;
				case 2:
					BillManager.saveBillLogsbyCompany_moi_id(SqlUtil.getCompanyMoi_id(room_moi_id));
					System.out.println(room_moi_id);
					BLogs = BillManager.getFalseBillLogsbyRoom_moi_id(room_moi_id,pay_type);
					if (BLogs == null || BLogs.size() == 0) {
						tableViewer.setInput(BLogs);
						MessageDialog.openInformation(PayAddComposite.this.getShell(), "通知",
								"没有找到未付的账单！");
					} else {
						tableViewer.setInput(BLogs);
					}
					break;
				case 3:
					BillManager.saveBillLogsbyCompany_moi_id(SqlUtil.getCompanyMoi_id(room_moi_id));
					BLogs = BillManager.getFalseBillLogsbyRoom_moi_id(room_moi_id,pay_type);
					if (BLogs == null || BLogs.size() == 0) {
						tableViewer.setInput(BLogs);
						MessageDialog.openInformation(PayAddComposite.this.getShell(), "通知",
								"没有找到未付的账单！");
					} else {
						tableViewer.setInput(BLogs);
					}
					break;
				default:
					break;
				}
			}
		});

		new Label(payComposite, SWT.NONE);
		// 交费按钮
		payButton = new Button(payComposite, SWT.NONE);
		payButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		payButton.setText("交费");
		payButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ArrayList<BillLog>  billLogs  = (ArrayList<BillLog>)tableViewer.getInput();
				if (billLogs != null && billLogs.size()>0) {
					if (BillManager.payFalseBill(billLogs)) {
						MessageDialog.openInformation(PayAddComposite.this.getShell(), "通知",
								"缴费全部成功！");
					}else {
						MessageDialog.openInformation(PayAddComposite.this.getShell(), "通知",
								"缴费部分成功，可能是由于余额不足以支付账单！\n您可以继续查询，查看哪些项目没有被扣费");
					}
				}else {
					MessageDialog.openInformation(PayAddComposite.this.getShell(), "通知",
							"缴费失败！");
				}
				tableViewer.setInput(null);
			}
		});

		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TableViewerColumn seqViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn seqTableColumn = seqViewerColumn.getColumn();
		seqTableColumn.setResizable(false);
		seqTableColumn.setAlignment(SWT.CENTER);
		seqTableColumn.setWidth(40);
		seqTableColumn.setText("序号");

		TableViewerColumn nameViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn nameTableColumn = nameViewerColumn.getColumn();
		nameTableColumn.setAlignment(SWT.CENTER);
		nameTableColumn.setWidth(200);
		nameTableColumn.setText("公司名称");

		TableViewerColumn buildingViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn buildingTableColumn = buildingViewerColumn.getColumn();
		buildingTableColumn.setResizable(false);
		buildingTableColumn.setAlignment(SWT.CENTER);
		buildingTableColumn.setWidth(50);
		buildingTableColumn.setText("楼栋");

		TableViewerColumn floorViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn floorTableColumn = floorViewerColumn.getColumn();
		floorTableColumn.setResizable(false);
		floorTableColumn.setAlignment(SWT.CENTER);
		floorTableColumn.setWidth(50);
		floorTableColumn.setText("楼层");

		TableViewerColumn roomViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn roomTableColumn = roomViewerColumn.getColumn();
		roomTableColumn.setResizable(false);
		roomTableColumn.setAlignment(SWT.CENTER);
		roomTableColumn.setWidth(50);
		roomTableColumn.setText("房间");

		TableViewerColumn timeViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn timeTableColumn = timeViewerColumn.getColumn();
		timeTableColumn.setResizable(false);
		timeTableColumn.setAlignment(SWT.CENTER);
		timeTableColumn.setWidth(150);
		timeTableColumn.setText("结算时间");

		TableViewerColumn itemViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn itemTableColumn = itemViewerColumn.getColumn();
		itemTableColumn.setResizable(false);
		itemTableColumn.setAlignment(SWT.CENTER);
		itemTableColumn.setWidth(60);
		itemTableColumn.setText("结算项目");

		TableViewerColumn beginAmountViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn beginAmountTableColumn = beginAmountViewerColumn
				.getColumn();
		beginAmountTableColumn.setResizable(false);
		beginAmountTableColumn.setAlignment(SWT.CENTER);
		beginAmountTableColumn.setWidth(60);
		beginAmountTableColumn.setText("初始数");

		TableViewerColumn endAmountViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn endAmountTableColumn = endAmountViewerColumn.getColumn();
		endAmountTableColumn.setResizable(false);
		endAmountTableColumn.setAlignment(SWT.CENTER);
		endAmountTableColumn.setWidth(60);
		endAmountTableColumn.setText("目前数");

		TableViewerColumn amountViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn amountTableColumn = amountViewerColumn.getColumn();
		amountTableColumn.setResizable(false);
		amountTableColumn.setAlignment(SWT.CENTER);
		amountTableColumn.setWidth(60);
		amountTableColumn.setText("用量");

		TableViewerColumn moneyViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn moneyTableColumn = moneyViewerColumn.getColumn();
		moneyTableColumn.setResizable(false);
		moneyTableColumn.setAlignment(SWT.CENTER);
		moneyTableColumn.setWidth(60);
		moneyTableColumn.setText("金额(元)");

		TableViewerColumn bealflagViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn bealflagTableColumn = bealflagViewerColumn.getColumn();
		bealflagTableColumn.setResizable(false);
		bealflagTableColumn.setAlignment(SWT.CENTER);
		bealflagTableColumn.setWidth(80);
		bealflagTableColumn.setText("是否已付款");
		
		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
